Java syntax
part 6/23 Β· 86.1 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
This type of declaration has been available since J2SE 5.0. Static import declarations allow access to static members defined in another class, interface, annotation, or enum; without specifying the class name:
import static java.lang.System.out; //'out' is a static field in java.lang.System
public class HelloWorld {
public static void main(String[] args) {
/* The following line is equivalent to
System.out.println("Hi World!");
and would have been incorrect without the import declaration. */
out.println("Hello World!");
}
}
Import-on-demand declarations allow to import all the fields of the type:
import static java.lang.System.*;
/* This form of declaration makes all
fields in the java.lang.System class available by name, and may be used instead
of the import declaration in the previous example. */
Enum constants may also be used with static import. For example, this enum is in the package called screen:
public enum ColorName {
RED, BLUE, GREEN
};
It is possible to use static import declarations in another class to retrieve the enum constants:
import screen.ColorName;
import static screen.ColorName.*;
public class Dots {
/* The following line is equivalent to 'ColorName foo = ColorName.RED',
and it would have been incorrect without the static import. */
ColorName foo = RED;
void shift() {
/* The following line is equivalent to
if (foo == ColorName.RED) foo = ColorName.BLUE; */
if (foo == RED) foo = BLUE;
}
}
Operators
Operators in Java are similar to those in C++. However, there is no delete operator due to garbage collection mechanisms in Java, and there are no operations on pointers since Java does not support them. Another difference is that Java has an unsigned right shift operator (>>>), while C's right shift operator's signedness is type-dependent. Operators in Java cannot be overloaded.
| Precedence | Operator | Description | Associativity |
|---|---|---|---|
| 1 | () | Method invocation | Left-to-right |
| 1 | [] | Array access | Left-to-right |
| 1 | . | Class member selection | Left-to-right |
| 2 | ++ -- | Postfix increment and decrement | Left-to-right |
| 3 | ++ -- | Prefix increment and decrement | Right-to-left |
| 3 | + - | Unary plus and minus | Right-to-left |
| 3 | ! ~ | Logical NOT and bitwise NOT | Right-to-left |
| 3 | ( type ) val | Type cast | Right-to-left |
| 3 | new | Class instance or array creation | Right-to-left |
| 4 | * / % | Multiplication, division, and modulus (β¦ | Left-to-right |
| 5 | + - | Addition and subtraction | Left-to-right |
| 5 | + | String concatenation | Left-to-right |
| 6 | << >> >>> | Bitwise left shift, signed right shift⦠| Left-to-right |
| 7 | < <= | Relational "less than" and "less than o⦠| Left-to-right |
| 7 | > >= | Relational "greater than" and "greater⦠| Left-to-right |
| 7 | instanceof | Type comparison | Left-to-right |
| 8 | == != | Relational "equal to" and "not equal to" | Left-to-right |
| 9 | & | Bitwise and logical AND | Left-to-right |
| 10 | ^ | Bitwise and logical XOR (exclusive or) | Left-to-right |
| 11 | / | Bitwise and logical OR (inclusive or) | Left-to-right |
| 12 | && | Logical conditional-AND | Left-to-right |
| 13 | // | Logical conditional-OR | Left-to-right |
| 14 | c ? t : f | Ternary conditional (see ?: ) | Right-to-left |
| 15 | = | Simple assignment | Right-to-left |
| 15 | += -= | Assignment by sum and difference | Right-to-left |
| 15 | *= /= %= | Assignment by product, quotient, and re⦠| Right-to-left |
| 15 | <<= >>= >>>= | Assignment by bitwise left shift, signe⦠| Right-to-left |
| 15 | &= ^= /= | Assignment by bitwise AND, XOR, and OR | Right-to-left |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ